function [V,H,f] = Arnold(A,V,H,f,k,m);
%
%   Input:  A -- an n by n matrix
%           V -- an n by k orthogonal matrix
%           H -- a k  by k upper Hessenberg matrix
%           f -- an nonzero n vector
% 
%           with   AV = VH + fe_k' (if k > 1)
%
%           k -- a positive integer (k << n assumed)
%
%           m -- a positive integer (k < m << n assumed)
%          
%
%   Output: V -- an n by m orthogonal matrix
%           H -- a m by m upper Hessenberg matrix
%           f -- an n vector
%
% 
%           with   AV = VH + fe_k'
%
%           Leading k columns of V agree with input V
%
%           assures V'V = I_m with  DGKS correction
%
%
%   D.C. Sorensen
%   2 March 2000
% 
Av = 0;
    n = length(f);
    if (k == 1),
 
       beta = norm(f);
       if ( beta == 0) , f_is_zero_error = beta, return;  end
       Av = Av+1;

       v = f/beta;
       w = A*v;
       alpha = v'*w;
    

       f = w - v*alpha;
           c = v'*f;
           f = f - v*c;
           alpha = alpha + c;

       V(:,1) = v; H(1,1) = alpha;
   end

   for j = k+1:m,
       beta = norm(f);
       v = f/beta;
           
       H(j,j-1) = beta; 
       V(:,j)   = v;
   
       w = A*v;
       h = V(:,1:j)'*w;
       f = w - V(:,1:j)*h;
           c = V(:,1:j)'*f;
           f = f - V(:,1:j)*c;
           h = h + c;
   
       H(1:j,j) = h;
    Av = Av+1;
   end 
   Av
